package com.youlu.campus.admin.data.controller;

import com.youlu.campus.admin.auth.entity.SystemUser;
import com.youlu.campus.admin.auth.service.UserService;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.VO.req.UserJoinActivityDataQueryVO;
import com.youlu.campus.service.data.UserJoinActivityDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;
import java.util.Objects;

@Slf4j
@RestController
public class UserJoinActivityDataController {
    @Autowired
    private UserJoinActivityDataService userJoinActivityDataService;
    @Autowired
    private UserService userService;

    /**
     * 活动用户报名列表
     *
     * @param req
     * @return
     */
    @PostMapping("/activity/users/list")
    public RestResult shipImport(@RequestBody UserJoinActivityDataQueryVO req) {
        return RestResultBuilder.builder().success(userJoinActivityDataService.list(req)).build();
    }

    /**
     * 导入报名数据
     *
     * @param
     * @return
     */
    @PostMapping("/activity/sign-users/import")
    public RestResult shipImport(@RequestParam("file") MultipartFile file,
                                 @RequestParam(value = "activityId", required = false) String activityId) {
        try {
            String fileName = file.getOriginalFilename();
            log.info(":>>> 导入报名数据:{}", fileName);
            Map<String, Object> res = userJoinActivityDataService.dataImport(file.getBytes(), fileName, activityId);
            return RestResultBuilder.builder().success(res).build();
        } catch (Exception e) {
            log.info(":>>> 导入导入报名数据记录错误:{}", e);
            return RestResultBuilder.builder().failure().build();
        }
    }

    /**
     *
     *
     * @param
     * @param
     */
    @PostMapping("/activity/sign-users/export")
    public RestResult orderExport(@RequestBody UserJoinActivityDataQueryVO req) {
        req.setUserId(AuthUtils.getUserId());
        SystemUser systemUser = userService.get(req.getUserId());
        if (Objects.nonNull(systemUser)) {
            req.setOptUserName(systemUser.getLoginName());
        }
        String result = userJoinActivityDataService.export(req);
        log.info(":>>> 导出报名信息结果:{}", result);
        if (StringUtils.isNotBlank(result)) {
            return RestResultBuilder.builder().success(result).build();
        }
        return RestResultBuilder.builder().failure().build();
    }
}
